//
//  HWLNavigationTitleFlowLayout.m
//  texttttt
//
//  Created by 黄威 on 16/8/30.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "HWLNavigationTitleFlowLayout.h"


@interface HWLNavigationTitleFlowLayout ()

@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *>* attributesArray;
@property (nonatomic, assign) CGFloat totalWidth;

@end

@implementation HWLNavigationTitleFlowLayout

- (instancetype)init{

    self = [super init];
    
    if (self) {
        _attributesArray = [NSMutableArray array];
        _totalWidth = 0.0f;
    }

    return self;

}

-(void)prepareLayout{

    if (_attributesArray.count != 0) {
        [_attributesArray removeAllObjects];
    }
    
    for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        
        NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
        
        UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index];
        CGFloat height = 40;
        CGFloat width = [self.delegate collectionView:self.collectionView layout:self WithForItemAtIndexPath:index height:height];
        CGFloat y =  0;
        CGFloat x = _totalWidth;
        _totalWidth = _totalWidth + width + 25;
        NSLog(@"%lf", _totalWidth);
        layoutAttributes.frame = CGRectMake(x, y, width, height);
        [_attributesArray addObject:layoutAttributes];
    }




}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    return _attributesArray;
}

- (CGSize)collectionViewContentSize {
    
    return CGSizeMake(_totalWidth, 0);
}

@end
